package com.gs.synedocket.presenter;


import com.google.gson.reflect.TypeToken;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.apiresult.LoginData;
import com.gs.synedocket.db.dto.LoginDto;
import com.gs.synedocket.model.LoginModel;
import com.gs.synedocket.model.listener.OnResultListener;
import com.gs.synedocket.view.ILoginView;

public class LoginPresenter extends BasePresenter<ILoginView> {
    private LoginModel loginModel = new LoginModel();

    public void login() {
        LoginDto bean = new LoginDto();
        bean.setUserName(getView().getUserName());
        bean.setPassword(getView().getEncryptPassword());
        bean.setValidateCode("test");
        loginModel.login(bean, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onLoginSuccess((ApiResultData<LoginData>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onLoginFail(e);
                }
            }
        }, new TypeToken<ApiResultData<LoginData>>() {
        }.getType());
    }

    public void getKey() {
        loginModel.getKey( new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onGetKeySuccess((ApiResultData<String>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetKeyFail(e);
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }

    public void reset(String userName) {
        getView().showLoading();
        loginModel.reset(userName, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onResetSuccess((ApiResultData<String>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onResetFail(e);
                }
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }
}
